package com.example.testone.viewModels

import android.annotation.SuppressLint
import android.app.AlertDialog
import android.app.Application
import android.content.Context
import android.content.DialogInterface
import android.view.View
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.MutableLiveData
import androidx.navigation.findNavController
import androidx.navigation.fragment.findNavController
import com.example.testone.R
import com.example.testone.utils.MyLog

class SettingViewModel(val context: Application):AndroidViewModel(context) {

    val customTel = "服务热线：400 8166 298"

    companion object{
        const val TAG = "SettingViewModel"
        //默认时间
        const val WD_MIN = 25
        const val WD_MAX = 50
        const val WD_SETTING = 30

        const val TIME_MIN = 5
        const val TIME_MAX = 80
        const val TIME_SETTING = 30
        const val BOX_UP_MINUTE = 3  //盒子时间延长时间

        const val LIGHT_MIN = 1
        const val LIGHT_MAX = 10
        const val LIGHT_NUMBER = 5

        const val VOICE_NUMBER = 10
        const val WD_NUMBER = 0

        const val INIT_PWD = "6688"
        const val UV_DOWNTIME = 10

    }

    //当前点击的按钮
    var currentBtn:String = ""
    //是否处于通信状态
    var isOpening : MutableLiveData<Boolean> = MutableLiveData(false)
    //检测开机状态,验证是否
    var checkRecOpenTime:Int = 0 //硬件返还的时间
    var checkSendOpenTime:Int = 0  //app发送请求时间
    //开机状态
    var isBootState : MutableLiveData<Boolean> = MutableLiveData(false)
    //检测工作状态,验证是否
    var checkRecBootTime:Int = 0 //硬件返还的时间
    var checkSendBootTime:Int = 0  //app发送请求时间
    //是否working状态
    var isWorking : MutableLiveData<Boolean> = MutableLiveData(false)
    //检测工作状态,验证是否
    var checkRecWorkTime:Int = 0 //硬件返还的时间
    var checkSendWorkTime:Int = 0  //app发送请求时间
    //开始状态
    val bootState: MutableLiveData<Int> = MutableLiveData(-1)
    //工作状态 0-停止 1-进行中 2-已暂停
    val settingRunningState: MutableLiveData<Int> = MutableLiveData(-1)
    //设置温度
    val wdSetting: MutableLiveData<Int> = MutableLiveData(-1)

    //设置时间-分钟
    //设置模式 -1系统设置 0常规设置
    val timeSetting: MutableLiveData<Int> = MutableLiveData(-1)
    val countdownState: MutableLiveData<Int> = MutableLiveData(-1)
    val countdownTime: MutableLiveData<Int> = MutableLiveData(0)
    //温度
    val wdNumber: MutableLiveData<Int> = MutableLiveData(-1)
    //灯光
    val lightNumber: MutableLiveData<Int> = MutableLiveData(-1)
    //声音
    val voiceNumber: MutableLiveData<Int> = MutableLiveData(-1)

    //各种开关状态 -1-初始状态  0-关闭 1-开启
    ////UV灯
    val switch_UV: MutableLiveData<Int> = MutableLiveData(-1)
    //ROOF灯
    val switch_ROOF: MutableLiveData<Int> = MutableLiveData(-1)
    //PWM加热
    val switch_HEAT_PWM: MutableLiveData<Int> = MutableLiveData(-1)
    //PAN加热
    val switch_HEAT_PAN: MutableLiveData<Int> = MutableLiveData(-1)
    //加热开
    val switch_HEAT: MutableLiveData<Int> = MutableLiveData(-1)
    //PAN开
    val switch_PAN: MutableLiveData<Int> = MutableLiveData(-1)
    //颜色
    val switch_COLOR: MutableLiveData<Int> = MutableLiveData(-1)
    //状态
    val switch_STATE: MutableLiveData<Int> = MutableLiveData(-1)


    //倒计时
    //密码
    var password : MutableLiveData<String> = MutableLiveData(INIT_PWD)
    var checkComplete : MutableLiveData<Int> = MutableLiveData(0)

    //显示那个板块
    val showMode: MutableLiveData<String> = MutableLiveData("password")

    //    var binding:DialogCustomPwdFragmentBinding? = null
    @SuppressLint("StaticFieldLeak")
//    var countdown:CountdownView? = null
    //工作状态 -1-初始化  0-停止 1-进行中 2-已暂停
    val countdownIsRunning: MutableLiveData<Int> = MutableLiveData(-1)


    //命令集合
    val sendCommandList:MutableLiveData<List<String>> = MutableLiveData(arrayListOf())
    val recCommandList:MutableLiveData<List<String>> = MutableLiveData(arrayListOf())


    //多功能按钮
    fun handleMultiOpt(v: View, type: String){
        MyLog.print(TAG, "type:${type};setruningState:${settingRunningState.value}")
        currentBtn = type;
        if(type=="suspend"){
            if ( settingRunningState.value==1 ) { //进行中
                settingRunningState.value = 2 //暂停
            }else{
                settingRunningState.value = 1
            }

        }else if(type=="stop"){ //停止
            AlertDialog.Builder(v.context).apply {
                setTitle("停止")
                setMessage("是否停止工作")
                setPositiveButton("停止", object: DialogInterface.OnClickListener{
                    override fun onClick(dialog: DialogInterface?, which: Int) {
                        //设备暂停
                        MyLog.print(TAG,"settingRunningState.settingCountdown.setOnCountdownEndListener:${ settingRunningState.value}")
                        settingRunningState.value = 2
                        v.findNavController().navigate(R.id.action_settingFragment_to_mainFragment)
                    }
                })
                setNegativeButton("取消",object: DialogInterface.OnClickListener{
                    override fun onClick(dialog: DialogInterface?, which: Int) {
                        dialog?.dismiss()
                    }
                })
            }.show()
        }else if(type=="wdPlus"){ //温度增加
            if(wdSetting.value!! < WD_MAX){
                wdSetting.value = wdSetting.value?.plus(1)
            }
        }else if(type=="wdSub"){ //温度减少
            if(wdSetting.value != null && wdSetting.value!! > WD_MIN){
                wdSetting.value = wdSetting.value!!.minus(1)
            }
        }else if(type=="timePlus"){ //时间增加
            if(timeSetting.value!! < TIME_MAX){
                timeSetting.value = timeSetting.value?.plus(1)
            }
        }else if(type=="timeSub"){ //时间减少
            if(timeSetting.value != null && timeSetting.value!! > TIME_MIN){
                timeSetting.value = timeSetting.value!!.minus(1)
            }
        }else if(type=="voicePlus"){ //声音增加
            voiceNumber.value = voiceNumber.value?.plus(1)
        }else if(type=="voiceSub"){ //声音减少
            if(voiceNumber.value != null && voiceNumber.value!! >=1){
                voiceNumber.value = voiceNumber.value!!.minus(1)
            }
        }else if(type=="lightPlus"){ //灯光增加
            if(lightNumber.value!! < LIGHT_MAX) {
                lightNumber.value = lightNumber.value?.plus(1)
            }
        }else if(type=="lightSub"){ //灯光减少
            if(lightNumber.value != null && lightNumber.value!! > LIGHT_MIN){
                lightNumber.value = lightNumber.value!!.minus(1)
            }
        }

    }

    fun navToTip (v: View) {
        try{
            if(isOpening.value==true){
                //开机
                bootState.value = 1
            }
            v.findNavController().navigate(R.id.action_mainFragment_to_tipFragment)
        }catch (e:Exception){}
    }

    fun navToSetting (v: View) {
        try{
            if(isOpening.value==true){
                //开机
                bootState.value = 1

            }
            v.findNavController().navigate(R.id.action_tipFragment_to_settingFragment)
        }catch (e:Exception){}
    }
}